package st.business.tepth.business.user.busnumber.workholder.repair.work.content;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.CustomListener;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import st.business.tepth.R;
import st.business.tepth.base.OnItemChooseListener;
import st.business.tepth.business.bean.OperationBean;
import st.business.tepth.business.bean.PartBean;
import st.business.tepth.business.bean.PartLocationBean;
import st.business.tepth.business.bean.PickNumberBean;
import st.business.tepth.business.bean.ProgramBean;
import st.business.tepth.business.user.busnumber.workholder.adapter.TroubleProgramAdapter;
import st.business.tepth.business.user.busnumber.workholder.dialog.DialogActionChoose;
import st.business.tepth.business.user.busnumber.workholder.dialog.DialogNumberSelect;
import st.business.tepth.business.user.busnumber.workholder.dialog.DialogPartChoose;
import st.business.tepth.business.user.busnumber.workholder.dialog.DialogPartLocationChoose;
import st.business.tepth.config.ItemFields;
import st.business.tepth.manager.GlobalDataManager;
import st.shadow.library.core.util.utilcode.util.ToastUtils;
import st.shadow.library.ui.recycler.MultipleItemEntity;

import static st.business.tepth.business.user.busnumber.workholder.adapter.TroubleProgramAdapter.TYPE_MANUAL_UN_CONFIRM;
import static st.business.tepth.business.user.busnumber.workholder.adapter.TroubleProgramAdapter.TYPE_RELEVANCE_UN_CONFIRM;
import static st.business.tepth.business.user.busnumber.workholder.adapter.TroubleProgramAdapter.TYPE_SYSTEM_RECOMMEND_UN_CONFIRM;

/**
 * @author zhangyu
 * @date 2019/8/2
 */
public class ProgramUiDelegate {
    private RecyclerView mRecyclerViewWorkProgram;
    private TroubleProgramAdapter mProgramAdapter;
    private Context mContext;
    private int mRepairId;
    private OptionsPickerView pvCustomOptions;

    public ProgramUiDelegate(Context context,
                             int repairId,
                             RecyclerView recyclerViewWorkProgram) {
        mRecyclerViewWorkProgram = recyclerViewWorkProgram;
        mContext = context;
        mRepairId = repairId;
        mRecyclerViewWorkProgram.setLayoutManager(new LinearLayoutManager(context));
        mRecyclerViewWorkProgram.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                MultipleItemEntity entity = (MultipleItemEntity) adapter.getItem(position);
                if (entity != null) {
                    int type = entity.getItemType();
                    int id = view.getId();
                    if (id == R.id.tv_action) {
                        //动作
                        chooseAction(adapter, view, position);
                    } else if (id == R.id.tv_obj || id == R.id.tv_part_code) {
                        //零部件和编码
                        choosePart(adapter, view, position, true);
                    } else if (id == R.id.tv_count) {
                        //零部件数量
                        choosePartCount(adapter, view, position);
                    } else if (id == R.id.tv_materials || id == R.id.tv_materials_code) {
                        //耗材和编码
                        choosePart(adapter, view, position, false);
                    } else if (id == R.id.tv_materials_count) {
                        //耗材数量
                        chooseGoodsCount(adapter, view, position);
                    } else if (id == R.id.tv_place) {
                        //位置
                        choosePartLocation(adapter, view, position);
                    } else if (id == R.id.tv_delete) {
                        if (type == TYPE_MANUAL_UN_CONFIRM
                                || type == TYPE_SYSTEM_RECOMMEND_UN_CONFIRM
                                || type == TYPE_RELEVANCE_UN_CONFIRM) {
                            adapter.remove(position);
//                            if (mUiDelegate != null) {
//                                mUiDelegate.changeState();
//                            }
                        }
                    }
                }
            }
        });
        mProgramAdapter = new TroubleProgramAdapter(context, null,
                new TroubleProgramAdapter.onItemOperatorListener() {

                    @Override
                    public void onOperator(final ProgramBean program, String status, final int position) {
//                        mDialogLoader.showWarning(status, "请确认",
//                                new IDialogClickListener() {
//                                    @Override
//                                    public void onClick() {
//                                        int pickStatus = program.getPicking_status();
//                                        if ((pickStatus >= 6 && pickStatus <= 9) ||
//                                                pickStatus == 0) {
//                                            //减去header
//                                            deleteProgram(program, position - 1);
//                                        } else if (pickStatus == 3 || pickStatus == 5) {
//                                            //退料
//                                            returnGoods(program, position - 1);
//                                        }
//                                    }
//                                });
                        //删除方案
                    }
                }, new TroubleProgramAdapter.onItemOperatorListener() {
            @Override
            public void onOperator(final ProgramBean program, String status, final int position) {
                //添加共同作业人
//                mDialogLoader.showWarning("添加我为共同作业人", "请确认",
//                        new IDialogClickListener() {
//                            @Override
//                            public void onClick() {
//                                mPresenter.addCoWorker(UserManager.getInstance().getToken(),
//                                        program.getId(), position - 1);
//                                mDialogLoader.changeToProgressType("正在提交...");
//                            }
//                        });
            }

        }, new TroubleProgramAdapter.onItemOperatorListener() {
            @Override
            public void onOperator(final ProgramBean program, String status, final int position) {
                //取消共同作业人
//                mDialogLoader.showWarning("取消我为共同作业人", "请确认",
//                        new IDialogClickListener() {
//                            @Override
//                            public void onClick() {
//
//                                mPresenter.cancelCoWorker(UserManager.getInstance().getToken(),
//                                        program.getId(), position - 1);
//                                mDialogLoader.changeToProgressType("正在提交...");
//                            }
//                        });
            }
        });
        mProgramAdapter.addHeaderView(LayoutInflater.from(mContext)
                .inflate(R.layout.item_head_work_program,
                        null));
        mRecyclerViewWorkProgram.setAdapter(mProgramAdapter);
    }

    /**
     * 选择动作
     */
    private void chooseAction(final BaseQuickAdapter adapter, final View view, final int position) {
        new DialogActionChoose(mContext, GlobalDataManager.getInstance().getOperationBeans(),
                new OnItemChooseListener<OperationBean>() {

                    @Override
                    public void onChoose(OperationBean operationBean) {
                        MultipleItemEntity entity = (MultipleItemEntity) adapter.getItem(position);
                        if (entity != null) {
                            ProgramBean diagnoseBean = entity.getField(ItemFields.ITEM_OBJ);
                            diagnoseBean.setAction_name(operationBean.getAction_name());
                            diagnoseBean.setMaintenance_action_id(operationBean.getId());
                            ((TextView) view).setText(operationBean.getAction_name());
                        }
                    }
                }).show();
    }

    /**
     * 选择零部件和编码
     */
    private void choosePart(final BaseQuickAdapter adapter, final View view,
                            final int position,
                            final boolean isPart) {
        new DialogPartChoose(mContext, mRepairId,
                new OnItemChooseListener<PartBean>() {
                    @Override
                    public void onChoose(PartBean partBean) {
                        MultipleItemEntity entity = (MultipleItemEntity) adapter.getItem(position);
                        if (entity != null) {
                            ProgramBean diagnoseBean = entity.getField(ItemFields.ITEM_OBJ);
                            TextView tv = ((TextView) view);
                            if (isPart) {
                                diagnoseBean.setPart(partBean);
                                if (view.getId() == R.id.tv_obj) {
                                    //零件
                                    tv.setText(partBean.getMaterial_name());
                                    TextView tvCode = (TextView) adapter.getViewByPosition(mRecyclerViewWorkProgram,
                                            position + 1, R.id.tv_part_code);
                                    if (tvCode != null) {
                                        tvCode.setText(partBean.getMaterial_no());
                                    }
                                } else {
                                    //编号
                                    tv.setText(partBean.getMaterial_no());
                                    TextView tvName = (TextView) adapter.getViewByPosition(mRecyclerViewWorkProgram,
                                            position + 1, R.id.tv_obj);
                                    if (tvName != null) {
                                        tvName.setText(partBean.getMaterial_name());
                                    }
                                }
                                TextView tvUnit = (TextView) adapter.getViewByPosition(mRecyclerViewWorkProgram,
                                        position + 1, R.id.tv_unit);
                                if (tvUnit != null) {
                                    tvUnit.setText(partBean.getUnit() == null ? "无" : partBean.getUnit());
                                }
                            } else {
                                diagnoseBean.setMaterial(partBean);
                                if (view.getId() == R.id.tv_materials) {
                                    //耗材
                                    tv.setText(partBean.getMaterial_name());
                                    TextView tvCode = (TextView) adapter.getViewByPosition(mRecyclerViewWorkProgram,
                                            position + 1, R.id.tv_materials_code);
                                    if (tvCode != null) {
                                        tvCode.setText(partBean.getMaterial_no());
                                    }
                                } else {
                                    //编号
                                    tv.setText(partBean.getMaterial_no());
                                    TextView tvName = (TextView) adapter.getViewByPosition(mRecyclerViewWorkProgram,
                                            position + 1, R.id.tv_materials);
                                    if (tvName != null) {
                                        tvName.setText(partBean.getMaterial_name());
                                    }
                                }
                            }
                        }
                    }
                }, isPart).measureSize().show();

    }

    /**
     * 零部件数量选择
     */
    private void choosePartCount(final BaseQuickAdapter adapter, final View view, final int position) {
        pvCustomOptions = new OptionsPickerBuilder(mContext,
                new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int option2, int options3, View v) {
                        //返回的分别是三个级别的选中位置
                        MultipleItemEntity entity = (MultipleItemEntity) adapter.getItem(position);
                        if (entity != null) {
                            ProgramBean programBean = entity.getField(ItemFields.ITEM_OBJ);
                            if (programBean != null) {
                                PartBean partBean = programBean.getPart();
                                if (partBean != null) {
                                    partBean.setVal(options1 + 1);
                                    ((TextView) view).setText(String.format(
                                            Locale.CHINA, "%d", options1 + 1));
                                } else {
                                    ToastUtils.showShort("请先选择对象");
                                }
                            }
                        }
                    }
                })
                .setLayoutRes(R.layout.dialog_number_select, new CustomListener() {
                    @Override
                    public void customLayout(View v) {
                        v.findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomOptions.dismiss();
                            }
                        });
                        v.findViewById(R.id.tv_confirm).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomOptions.returnData();
                                pvCustomOptions.dismiss();
                            }
                        });
                    }
                })
                .isDialog(true)
                .setOutSideCancelable(false)
                .build();
        List<PickNumberBean> list = new ArrayList<>();
        for (int i = 1; i < 50; i++) {
            list.add(new PickNumberBean(i + ""));
        }
        //noinspection unchecked
        pvCustomOptions.setPicker(list);//添加数据
        pvCustomOptions.show();
    }

    /**
     * 耗材数量选择
     */
    private void chooseGoodsCount(BaseQuickAdapter adapter, final View view, int position) {
        MultipleItemEntity entity = (MultipleItemEntity) adapter.getItem(position);
        if (entity != null) {
            ProgramBean diagnoseBean = entity.getField(ItemFields.ITEM_OBJ);
            final PartBean partBean = diagnoseBean.getMaterial();
            if (partBean == null) {
                ToastUtils.showShort("请先选择耗材");
            } else {
                new DialogNumberSelect(mContext,
                        new OnItemChooseListener<String>() {
                            @Override
                            public void onChoose(String s) {
                                partBean.setVal(Integer.parseInt(s));
                                ((TextView) view).setText(String.format("%s%s", s, partBean.getUnit() == null ? "L" :
                                        partBean.getUnit()));
                            }
                        }).measureSize().show();

            }
        }
    }

    /**
     * 位置选择
     *
     * @param adapter
     * @param view
     * @param position
     */
    private void choosePartLocation(final BaseQuickAdapter adapter, final View view, final int position) {
        new DialogPartLocationChoose(mContext,
                new DialogPartLocationChoose.OnLocationChooseListener() {

                    @Override
                    public void onChoose(PartLocationBean partLocationBean, String temp) {
                        if (temp.isEmpty()) {
                            //没有临时添加的位置
                            MultipleItemEntity entity = (MultipleItemEntity) adapter.getItem(position);
                            if (entity != null) {
                                ProgramBean programBean = entity.getField(ItemFields.ITEM_OBJ);
                                programBean.setPositionName(partLocationBean.getPositionName());
                                programBean.setPositionCode(partLocationBean.getPositionCode());
                                //diagnoseBean.setAction_name(operationBean.getAction_name());
                                // diagnoseBean.setMaintenance_action_id(operationBean.getId());
                                ((TextView) view).setText(partLocationBean.getPositionName());
                            }
                        } else {
                            //有临时添加的位置
//                            mDialogLoader.showProgressDialog(false, "正在添加",
//                                    "请稍候...");
//                            mPresenter.addTempLocation(UserManager.getInstance().getToken(),
//                                    position, partLocationBean, temp);

                        }

                    }
                }).measureSize().show();
    }
}
